package xian.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xian.woniu.entity.Doctor;
import xian.woniu.entity.DoctorExample;
import xian.woniu.entity.Orders;
import xian.woniu.entity.Patient;
import xian.woniu.mapper.DoctorMapper;
import xian.woniu.mapper.custom.CustomDoctorMapper;
import xian.woniu.service.IDoctorService;
import xian.woniu.service.IOrdersService;
import xian.woniu.service.IPatientService;
import xian.woniu.util.ResultVO;

import java.util.Collections;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author SongHang
 * @time 2022/06/29 14:47:45
 */
@Transactional
@Service
public class DoctorServiceImpl implements IDoctorService {
    private CustomDoctorMapper doctorMapper;
    private IOrdersService iOrdersService;
    private IPatientService iPatientService;

    public DoctorServiceImpl(CustomDoctorMapper doctorMapper,IOrdersService iOrdersService,IPatientService iPatientService) {
        this.doctorMapper = doctorMapper;
        this.iOrdersService=iOrdersService;
        this.iPatientService=iPatientService;
    }
@Override
    public List<Patient> showPatientInfoByDid(Integer did){

        System.out.println(iOrdersService.getPatientByDid(did).size());


        List<Orders> OrdersList = iOrdersService.getPatientByDid(did);

        if(OrdersList.size()==0||OrdersList==null){
            return null;
        }

        List<Patient> Patientlist=new ArrayList<>();
        for (int i=0;i<OrdersList.size();i++){
            Integer pid = OrdersList.get(i).getPid();
            ResultVO one = iPatientService.findOne(String.valueOf(pid));

//            注意回传回来的数据是Object对象 得处理，还得再转为对象
            Map map = (Map) one.getData();
            System.out.println(map);
            Patient patient = new Patient();
            try {
                BeanUtils.populate(patient, map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            if(!Patientlist.contains(patient)){
                Patientlist.add(patient);
            }

        }

        return Patientlist;
    }

    @Override
    public int save(Doctor doctor) {
        return doctorMapper.insertSelective(doctor);
    }

    @Override
    public int delete(Integer did) {
        return doctorMapper.deleteByPrimaryKey(did);
    }

    @Override
    public Doctor findOne(Integer did) {
        return doctorMapper.selectByPrimaryKey(did);
    }

    @Override
    public List<Doctor> findAll() {
        return doctorMapper.selectByExample(null);
    }

    @Override
    public int update(Doctor doctor) {
        return doctorMapper.updateByPrimaryKeySelective(doctor);
    }

    @Override
    public Doctor getDoctorByUid(Integer uid) {
        DoctorExample example = new DoctorExample();
//        设置查询条件。按照uid查找对应的医生信息
        example.or().andUidEqualTo(uid);
        List<Doctor> doctors = doctorMapper.selectByExample(example);
        return doctors != null && doctors.size() > 0 ? doctors.get(0) : null;
    }

    //带text的查所有
    @Override
    public List<Doctor> findAllWithText() {
        return doctorMapper.selectByExampleWithBLOBs(null);
    }

    @Override
    public List<Doctor> getwaittask() {
        List<Doctor> doctorList = doctorMapper.getwaittask();
        return doctorList;
    }

    @Override
    public List<Doctor> getDoctorByDids(List<Integer> did,Integer pageNow, Integer pageSize) {
        System.out.println(pageNow);
        System.out.println(pageSize);
        PageHelper.startPage(pageNow,pageSize);

        List<Doctor> doctor = doctorMapper.getDoctorByDids(did);
        return doctor;
    }
}
